#include "frm_register.h"
#include "ui_frm_register.h"
#include "api.h"
#include <QString>
Frm_Register::Frm_Register(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Frm_Register)
{
    ui->setupUi(this);
    //connect(ui->btn_register,&QPushButton::clicked,this,&Frm_Register::RequestForNewUser);
    connect(ui->btn_register,&QPushButton::clicked,this,&Frm_Register::OnRegisterDone);
    connect(ui->btn_vricode,&QPushButton::clicked,this,&Frm_Register::OnVerify);
    connect(&timer,&QTimer::timeout,this,&Frm_Register::TimeOut);
    timer.setInterval(1000);
}
// 按下注册按键，检查用户输入
void Frm_Register::OnRegisterDone(){
    if(ui->txt_pwd->text()!=ui->txt_repwd->text()){
        qDebug()<<"Error: PassWord not the same";
        return;
    }
    QString name = ui->txt_username->text();
    QString password = ui->txt_pwd->text();
    QString email = ui->txt_email->text();
    QString id = ui->txt_id->text();
    QString veri = ui->txt_verify->text();
    int age = ui->sp_age->value();
    Role t = ui->rbtn_child->isChecked()?child:ui->rbtn_elder->isChecked()?elderly:adult;
    //QString verifycode = ui->txt_verify->text();
    emit RequestForNewUser(new User(name,password,id,email,t,age), veri);
    //currentUser = u;
    //this->accept();
}

/**
 * @brief 发送验证码槽函数
 * 当用户点击发送验证码按钮时触发此函数
 */
void Frm_Register::TimeOut(){
    ui->btn_vricode->setText(QString::number(cnt));
    cnt--;
    if(cnt==-1){
        cnt = 60;
        timer.stop();
        ui->btn_vricode->setText("验证码");
        ui->btn_vricode->setEnabled(true);
    }
}
void Frm_Register::OnVerify(){
    QString email = ui->txt_email->text();
    emit RequestForVerify(email);
    ui->btn_vricode->setEnabled(false);
    timer.start();
}

Frm_Register::~Frm_Register()
{
    delete ui;
}
